package com.redpack.quick.service.impl;

import com.redpack.quick.datasource.dao.TaskDao;
import com.redpack.quick.datasource.entity.TaskEntity;
import com.redpack.quick.service.ITask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.Predicate;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by shuttle on 4/2/18.
 */
@Service
public class TaskImpl implements ITask {

    @Autowired
    TaskDao taskDao;

    @Override
    public Page<TaskEntity> findByPage(int pageSize, int pageNum) {
        List<TaskEntity> beanList = new ArrayList<>();
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = new PageRequest(pageNum, pageSize, sort);
//        Specification<TaskEntity> specification = new Specification<TaskEntity>() {
//            //构建查询条件，可扩展动态配置查询条件
//            @Override
//            public Predicate toPredicate(Root<TaskEntity> root,
//                                         CriteriaQuery<?> criteriaQuery,
//                                         CriteriaBuilder criteriaBuilder) {
//                Predicate predicate = criteriaBuilder.equal(root.get("enabled"), 1);
//                return criteriaBuilder.and(predicate);
//            }
//        };
        Page<TaskEntity> taskList = taskDao.findAll(pageable);
        return taskList;
    }

    @Override
    public TaskEntity findById(long id) {
        return taskDao.findOne(id);
    }
}
